package net.m510.autosurfer.kddi;

import java.net.URI;
import java.util.List;
import java.util.logging.Logger;

import net.m510.autosurfer.common.DocumentPostProcess;
import net.m510.autosurfer.common.exception.CannotLoginException;
import net.m510.autosurfer.core.DocumentChecker;
import net.m510.autosurfer.core.Parameters;
import net.m510.autosurfer.core.UserAgents;
import net.m510.autosurfer.core.exception.UserProcessException;
import net.m510.autosurfer.core.utils.AnalyzeUtils;

import org.apache.http.HttpHost;
import org.apache.http.NameValuePair;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.impl.client.CloseableHttpClient;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.FormElement;

/**
 * @author mgoto
 *
 */
public class KddiAuIdLogin extends DocumentPostProcess {
	private static final Logger log = Logger.getLogger(KddiAuIdLogin.class.getName());

	private String auId;
	private String password;
	
	public KddiAuIdLogin(CloseableHttpClient client, HttpClientContext context) {
		super(client, context);
		super.log = log;
	}
	@Override
	protected URI getUri(Document doc) {
//		String action = AnalyzeUtils.getFormElementByName(doc, "main").attr("action");
		String query ="/net/vwc/cca_lg_eu_nets/cca";
		HttpHost host = context.getTargetHost();
		URI uri = URI.create(host.toURI());
		uri = uri.resolve(query);
		return uri;
	}
	
	@Override
	protected List<NameValuePair> getRequestParameters(Document doc) {
		FormElement form = AnalyzeUtils.getFormElementByName(doc, "main");
		Parameters p = Parameters.create(form);
		p.put("loginAliasId", this.auId);
		p.put("loginAuonePwd", this.password);
		p.put("loginAuonePwdUnMask", this.password);
		return p.asNameValueList();
//		11/29/14 20:56:48 FINE KddiGoAuIdLogin [logForms] form[0].attrs: name="main" action="cca" method="POST" enctype="application/x-www-form-urlencoded" class="defaultForm"
//		11/29/14 20:56:48 FINE KddiGoAuIdLogin [logElements] f[0].p[0]:<input type="hidden" id="eventController" name="eventController" value="LGN.login.pc.Login" />
//		11/29/14 20:56:48 FINE KddiGoAuIdLogin [logElements] f[0].p[1]:<input type="hidden" id="eventMethod" name="event_doLogin" value="" />
//		11/29/14 20:56:48 FINE KddiGoAuIdLogin [logElements] f[0].p[2]:<input type="hidden" id="windowId" name="windowId" value="0" />
//		11/29/14 20:56:48 FINE KddiGoAuIdLogin [logElements] f[0].p[3]:<input type="hidden" id="seqNo" name="seqNo" value="6213474665059596914" />
//		11/29/14 20:56:48 FINE KddiGoAuIdLogin [logElements] f[0].p[4]:<input type="hidden" id="targeturl" name="targeturl" value="https://cs1.auone.jp/aus/au-support/top" />
//		11/29/14 20:56:48 FINE KddiGoAuIdLogin [logElements] f[0].p[5]:<input type="hidden" id="svc" name="svc" value="" />
//		11/29/14 20:56:48 FINE KddiGoAuIdLogin [logElements] f[0].p[6]:<input type="hidden" id="psk" name="psk" value="" />
//		11/29/14 20:56:48 FINE KddiGoAuIdLogin [logElements] f[0].p[7]:<input type="hidden" id="func" name="func" value="" />
//		11/29/14 20:56:48 FINE KddiGoAuIdLogin [logElements] f[0].p[8]:<input type="hidden" id="param" name="param" value="" />
//		11/29/14 20:56:48 FINE KddiGoAuIdLogin [logElements] f[0].p[9]:<input type="text" class="p_p_2" name="loginAliasId" id="loginAliasId" maxlength="32" value="" />
//		11/29/14 20:56:48 FINE KddiGoAuIdLogin [logElements] f[0].p[10]:<input type="password" class="p_p_2" name="loginAuonePwd" id="loginAuonePwd" maxlength="32" />
//		11/29/14 20:56:48 FINE KddiGoAuIdLogin [logElements] f[0].p[11]:<input type="test" class="p_p_2" name="loginAuonePwdUnMask" id="loginAuonePwdUnMask" maxlength="32" style="display:none" autocomplete="off" />
//		11/29/14 20:56:48 FINE KddiGoAuIdLogin [logElements] f[0].p[12]:<input checked="checked" id="loginStateHold" value="ON" name="loginStateHold" type="checkbox" />
//		11/29/14 20:56:48 FINE KddiGoAuIdLogin [logElements] f[0].p[13]:<input type="image" id="btn_login" name="btn_login" src="resources/image/common/btn_login.png" onclick="trEvent('','cca','A-310','A-310_ログイン');" onmouseover="this.src='resources/image/common/btn_login_o.png'" onmouseout="this.src='resources/image/common/btn_login.png'" alt="ログイン" align="middle" />
	}

	public String getAuId() {
		return auId;
	}
	public void setAuId(String auId) {
		this.auId = auId;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	
}